from django.forms.models import model_to_dict

# 话题脚本模型转字典
def script_topic_to_dict(topic):
    """
    将TopicScript模型转换为字典格式，用于API响应
    """
    topic_dict = model_to_dict(topic, exclude=['deleted'])
    topic_dict["script_id"] = str(topic.script_id)
    # 格式化状态字段
    topic_dict["status"] = {
        "code": topic.status,
        "message": topic.get_status_display(),
    }
    # 格式化时间字段
    topic_dict["create_time"] = topic.create_time.strftime("%Y-%m-%d %H:%M:%S")
    topic_dict["update_time"] = topic.update_time.strftime("%Y-%m-%d %H:%M:%S")
    # 包含分类名称信息
    topic_dict["category_name"] = topic.category.name if topic.category else ""
    return topic_dict